package com.shd.edanpei.presenter;

import android.text.TextUtils;

import com.shd.edanpei.R;
import com.shd.edanpei.base.BasePresenterImpl;
import com.shd.edanpei.base.data.RxBusCode;
import com.shd.edanpei.base.network.ApiParams;
import com.shd.edanpei.base.network.api.ApiCreator;
import com.shd.edanpei.base.network.rx.BaseObserver;
import com.shd.edanpei.base.network.rx.RxTransformer;
import com.shd.edanpei.base.rxbus.RxBus;
import com.shd.edanpei.bean.OnlinePayModel;
import com.shd.edanpei.bean.PayModel;
import com.shd.edanpei.bean.StatusBean;
import com.shd.edanpei.presenter.contract.OnlinePayContract;
import com.shd.edanpei.enums.StatusEnum;
import com.shd.edanpei.util.ToastUtil;
import com.shd.edanpei.util.pay.PayUtils;

import java.util.WeakHashMap;

import io.reactivex.android.schedulers.AndroidSchedulers;

/**
 * Created by mc on 2018/10/22.
 * yang-wen-wen
 */

public class OnlinePayPresenter extends BasePresenterImpl<OnlinePayContract.OnlinePayIView>
        implements OnlinePayContract.OnlinePayIPresenter {

    public OnlinePayPresenter(OnlinePayContract.OnlinePayIView mView) {
        super(mView);
    }

    @Override
    public void getPayData(int orderID) {
        ApiCreator.getApi().getOnlinePayData(orderID)
                .compose(RxTransformer.<OnlinePayModel>errorHandle())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<OnlinePayModel>() {
                    @Override
                    protected void onSuccess(OnlinePayModel data) {
                        mBaseIView.setPayData(data);
                    }
                });
    }

    @Override
    public void submit(int orderID, final int checkedRadioButtonId, final int index) {
        final WeakHashMap<String, Object> hashMap = new WeakHashMap<>();
        hashMap.put(ApiParams.ORDER_ID, orderID);
        hashMap.put(ApiParams.PAY_TYPE, checkedRadioButtonId == R.id.rbt_alipay ? "alipay" : "wechat");
        ApiCreator.getApi().payService(hashMap)
                .compose(RxTransformer.<PayModel>errorHandle())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<PayModel>() {
                    @Override
                    protected void onSuccess(PayModel data) {
                        if (TextUtils.isEmpty(data.getErrorMsg())) {
                            if (checkedRadioButtonId == R.id.rbt_alipay) {
                                //支付宝支付
                                PayUtils.getInstance().aliPay(mBaseIView.getContext(), data.getAlipay(), new PayUtils.AliPayListener() {
                                    @Override
                                    public void success() {
                                        RxBus.get().send(RxBusCode.REFRESH_ORDER_STATUS, new StatusBean(index, StatusEnum.waitLoan.getStatus()));
                                        ToastUtil.showToast("支付成功");
                                        mBaseIView.getContext().finish();
                                    }

                                    @Override
                                    public void failed() {
                                        ToastUtil.showToast(PayUtils.PAY_FAILED);
                                    }
                                });
                            }
                            if (checkedRadioButtonId == R.id.rbt_wechat) {
                                //微信支付
//                                mPaymentEnum = paymentEnum;
//                                mPayType = Constant.PayType.service;
//                                mOrderId = orderId;
//                                mIndex = index;
                                PayUtils.getInstance().wechatPay(mBaseIView.getContext(), data.getWechat());
                            }
                        } else {
                            ToastUtil.showToast(data.getErrorMsg());
                        }
                    }
                });
    }
}
